<!-- 开支编辑弹窗 -->
<template>
  <ele-page>
    <ele-card>
      <template #header> 所属企业信息 </template>
      <el-form ref="formRef" :model="form" inline label-width="120px">
        <el-form-item label="企业" prop="company">
          <el-input
            clearable
            v-model="form.company"
            placeholder="请选择"
            style="width: 300px"
            readonly
            @click="openCompany"
          />
        </el-form-item>

        <el-form-item label="岗位" prop="position_id">
          <el-select
            v-model="form.position_id"
            class="mr"
            placeholder="请选择"
            style="width: 300px"
          >
            <el-option
              v-for="(item, index) in positionList"
              :key="item.id"
              :label="item.title"
              :value="item.id"
              @click="handlePositionChange(index)"
            />
          </el-select>
        </el-form-item>
        <br />
        <!-- 快捷月份选择 -->
        <el-form-item label="开支所属月份">
          <el-date-picker
            v-model="form.belongs_date"
            type="month"
            placeholder="选择月份"
            style="width: 150px"
            value-format="YYYY-MM"
          />
        </el-form-item>
      </el-form>
    </ele-card>
    <ele-card>
      <template #header> 开支详情 </template>
      <Cost ref="costRef" />
      <template #footer>
        <el-button type="primary" @click="save">保存</el-button>
      </template>
    </ele-card>
    <!-- 选择企业 -->
    <Choose v-model="showCompany" @done="chooseCompanyDone" />
  </ele-page>
</template>

<script setup>
  import { ref } from 'vue';
  import { EleMessage } from 'ele-admin-plus/es';
  import { useFormData } from '@/utils/use-form-data';
  import { addCompanyCost, pagePosition } from '@/api/company';
  import Choose from '../components/chooseCompany.vue';
  import Cost from './components/cost.vue';

  const costRef = ref(null);

  /** 提交状态 */
  const loading = ref(false);

  /** 表单实例 */
  const formRef = ref(null);

  /** 表单数据 */
  const [form] = useFormData({
    site_id: '',
    tenant_id: '',
    company_id: '',
    company: '',
    position: '',
    position_id: '',
    belongs_date: ''
  });

  /** 当前所选岗位信息 */
  const currentPosition = ref([]);
  const company = ref([]);
  /** 选择企业 */
  const showCompany = ref(false);
  const openCompany = () => {
    showCompany.value = true;
  };
  const chooseCompanyDone = (e) => {
    form.company = e.company_name;
    form.company_id = e.id;
    form.tenant_id = e.tenant_id;
    form.site_id = e.site_id;
    company.value = e;
    // form.position = '';
    form.position_id = '';
    showCompany.value = false;
    getCompanyPosition();
  };

  /** 职位 */
  const positionList = ref([]);
  const getCompanyPosition = () => {
    pagePosition({
      status: 1,
      company_id: form.company_id,
      page: 1,
      limit: 99999999
    })
      .then((res) => {
        positionList.value = res.list;
      })
      .catch((e) => {
        EleMessage.error(e.message);
      });
  };

  /** 选择职位后 **/
  const handlePositionChange = (i) => {
    form.position = positionList.value[i].title;
    currentPosition.value = positionList.value[i];
  };

  /** 保存编辑 */
  const save = () => {
    if (!form.belongs_date) {
      EleMessage.error('请选择月份');
      return;
    }
    if (form.company_id == '') {
      EleMessage.error('请选择项目');
      return;
    }
    formRef.value?.validate?.((valid) => {
      if (!valid) {
        return;
      }
      loading.value = true;
      var requestData = Object.assign({}, form);
      requestData.costList = costRef.value
        ? JSON.stringify(costRef.value.list)
        : '';

      addCompanyCost(requestData)
        .then((msg) => {
          loading.value = false;
          EleMessage.success(msg);
          form.belongs_date = '';
          form.company = '';
          form.company_id = '';
          form.position = '';
          form.position_id = '';
          costRef.value.list = [
            {
              cate: '',
              amount: '',
              remark: '',
              images: ''
            }
          ];
        })
        .catch((e) => {
          loading.value = false;
          EleMessage.error(e.message);
        });
    });
  };
</script>
